<template>
  <div class="admin-users">
    <div class="page-header">
      <h2 class="page-title">用户管理</h2>
    </div>

    <!-- 用户列表 -->
    <el-table
      :data="userList"
      style="width: 100%"
      v-loading="loading"
    >
      <el-table-column prop="username" label="用户名" min-width="120" />
      <el-table-column prop="email" label="邮箱" min-width="180" />
      <el-table-column prop="userType" label="角色" width="120">
        <template #default="{ row }">
          <el-tag :type="getRoleType(row.userType)">
            {{ getRoleText(row.userType) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="注册时间" width="180" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="row.status === 0 ? 'success' : 'danger'">
            {{ row.status === 0 ? '正常' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-button 
            link 
            :type="row.status === 0 ? 'danger' : 'success'"
            @click="handleToggleStatus(row)"
          >
            {{ row.status === 0 ? '禁用' : '启用' }}
          </el-button>
          <el-button link type="primary" @click="handleResetPassword(row)">
            重置密码
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getUserList, updateUserStatus, resetUserPassword } from '../../api/user'

// 数据列表
const loading = ref(false)
const userList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 获取角色类型
const getRoleType = (userType) => {
  const typeMap = {
    '0': 'info',
    '1': 'warning',
    '2': 'danger'
  }
  return typeMap[userType] || 'info'
}

// 获取角色文本
const getRoleText = (userType) => {
  const textMap = {
    '0': '求职者',
    '1': '招聘者',
    '2': '管理员'
  }
  return textMap[userType] || '未知'
}

// 获取用户列表
const fetchUserList = async () => {
  loading.value = true
  try {
    const response = await getUserList({
      page: currentPage.value,
      size: pageSize.value
    })
    if (response.code === 200) {
      userList.value = response.data.records
      total.value = response.data.total
    } else {
      throw new Error(response.message)
    }
  } catch (error) {
    console.error('获取用户列表失败', error)
    ElMessage.error('获取用户列表失败')
  } finally {
    loading.value = false
  }
}

// 切换用户状态
const handleToggleStatus = async (row) => {
  try {
    const action = row.status === 0 ? '禁用' : '启用'
    await ElMessageBox.confirm(
      `确定要${action}该用户吗？`,
      '操作确认',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    const params = {
      id: row.id,
      status: row.status === 0 ? 1 : 0
    }
    console.log('发送的参数：', params)
    
    const response = await updateUserStatus(params)
    
    if (response.code === 200) {
      ElMessage.success(`${action}成功`)
      fetchUserList()
    } else {
      throw new Error(response.message)
    }
  } catch (error) {
    if (error !== 'cancel') {
      console.error('操作失败', error)
      ElMessage.error(error.message || '操作失败')
    }
  }
}

// 重置用户密码
const handleResetPassword = async (row) => {
  try {
    await ElMessageBox.confirm(
      '确定要重置该用户的密码吗？',
      '操作确认',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    const response = await resetUserPassword(row.id)
    if (response.code === 200) {
      ElMessage.success('密码重置成功')
    } else {
      throw new Error(response.message)
    }
  } catch (error) {
    if (error !== 'cancel') {
      console.error('密码重置失败', error)
      ElMessage.error(error.message || '密码重置失败')
    }
  }
}

// 分页处理
const handleSizeChange = (size) => {
  pageSize.value = size
  fetchUserList()
}

const handleCurrentChange = (page) => {
  currentPage.value = page
  fetchUserList()
}

// 初始化
onMounted(() => {
  fetchUserList()
})
</script>

<style scoped>
.admin-users {
  padding: var(--spacing-lg);
}

.admin-users .page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--spacing-lg);
}

.admin-users .page-header .page-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

.admin-users .pagination-container {
  margin-top: var(--spacing-lg);
  display: flex;
  justify-content: flex-end;
}

@media (max-width: 768px) {
  .admin-users {
    padding: var(--spacing-md);
  }

  .admin-users .page-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .admin-users .page-header .page-title {
    margin-bottom: var(--spacing-md);
  }
}
</style> 